﻿
@{
    ViewData["Title"] = "数据表字段信息";
    Layout = "~/Views/Shared/_Index.cshtml";
}

<div class="container-div">
    <input type="hidden" id="tableName" name="tableName" value="@(ViewBag.TableName)">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="searchForm">
                <div class="select-list">
                    <ul>

                        <li>
                            字段名称：<input type="text" id="columnName" name="columnName">
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" id="searchButton"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" id="resetButton"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <input type="hidden" id="tableName" name="tableName" value="@(ViewBag.TableName)"/>
        <div class="btn-group-sm" role="group" id="toolbar">
            <a class="btn btn-primary" onclick="syncTable(this);" data-id="0" data-role="system:codegen:synctable">
                <i class="fa fa-refresh"></i> 同步表结构
            </a>
            <a class="btn btn-info btn-xs " href="javascript:void(0)" data-id="0" onclick="genTable(this);" data-role="system:codegen:gentable">
            <i class="fa fa-search"></i>预览
            </a>
            @*<a class="btn btn-danger multiple disabled" onclick="del(this);" data-id="0" data-role="system:codegen:delete">
            <i class="fa fa-remove"></i> 删除
        </a>*@
            <span class="badge badge-primary">数据表名称：@(ViewBag.TableName)</span>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="dataTable"></table>
        </div>

    </div>
</div>
@section scripts
{
    <script>
        var $table;
        $(function () {
            loadGrid();
        });

        function loadGrid() {
            let options = {
                url: '/admin/CodeGen/getColumndata',
                pagination: false,
                queryParams: function (params) {
                    let p = {
                        tableName: $('#tableName').val(),
                        columnName: $('#columnName').val()
                    }
                    return p;
                },
                columns: [
                    { field: 'ck', checkbox: true },
                    {
                        field: 'column_name', title: '字段名称', align: 'left'
                    },
                    {
                        field: 'type_text_full', title: '数据库类型', align: 'left'
                    },
                    {
                        field: 'cstype', title: 'C#类型', align: 'left'
                    },
                    {
                        field: 'is_identity', title: '主键', align: 'left', formatter: function (v,r) {

                            if (v === 1 && r.is_identity == 1) {
                                return '<span class="badge badge-primary">主键自增</span>';
                            }

                            if (v === 1) {
                                return '<span class="badge badge-primary">主键</span>';
                            }
                           
                            return "";
                        }
                    },

                    {
                        field: 'can_null', title: '可空', align: 'left', formatter: function (v) {
                            if (v === 1) {
                                return '<span class="badge badge-primary">是</span>';
                            }
                            return '';
                        }
                    },
                    {
                        field: 'comment', title: '字段描述', align: 'left'
                    },
                    {
                        field: 'sync_time', title: '首次同步', align: 'left'
                    },
                    {
                        field: 'update_time', title: '最近同步', align: 'left'
                    },
                    {
                        field: 'id', title: '操作', align: 'left'
                    }
                ]

            };
            $table = $('#dataTable').easyTable(options);
        }

        function syncTable() {
            var tname = $('#tableName').val();
            let msg = '确认同步表【' + tname + '】的表结构吗？';
            jutils.confirm(msg, function () {
                jutils.ajaxGet('/admin/CodeGen/syncTable',
                    { tableName: tname },
                    function () {
                        $table.easyTable('search');
                    });
            })
        }


        function genTable(obj) {

            var tname = $('#tableName').val();

            jutils.dialogTop('代码预览【生成代码仅为辅助开发，请根据实际调整代码】', '/admin/codegen/gencode', { tableName: tname });

        }
        function query() {
            $table.easyTable('search');
        }
    </script>
}








